package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.base.result.R;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.service.UserAccountService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-01-04
 */
@RestController
@RequestMapping("/api/core/userAccount")
public class ApiUserAccountController {

    @Resource
    UserAccountService userAccountService;

    @ApiOperation("提现回调")
    @PostMapping("/notifyWithdraw")
    public String notifyWithdraw(HttpServletRequest request){
        return userAccountService.notifyWithdraw(request);
    }
    @ApiOperation("提现")
    @GetMapping("/auth/withdraw")
    public R withdraw(@RequestHeader(value = "token",required = false)String token,
                      BigDecimal amount){
        String form = userAccountService.withdraw(token,amount);
        return R.ok().data("form",form);
    }



    @ApiOperation("查询账户余额")
    @GetMapping("accountAmount")
    public R getAccountAmount(@RequestHeader(value = "token",required = false)String token){
        Long userId = JwtUtils.getUserId(token);
        UserAccount userAccount = userAccountService.getOne(Wrappers.lambdaQuery(UserAccount.class)
                .eq(UserAccount::getUserId, userId)
                .select(UserAccount::getAmount));
        return R.ok().data("amount",userAccount.getAmount())
                    .data("userId",userId);
    }

    //回调接口 /api/core/userAccount/notify
    @ApiOperation("充值回调")
    @PostMapping("notify")
    public String rechargeNotify(HttpServletRequest request){

        return userAccountService.rechargeNotify(request);
    }

    @ApiOperation("充值")
    @PostMapping("recharge")
    public R recharge(@RequestHeader(value = "token",required = false)String token,
                      BigDecimal amount){
        String form = userAccountService.recharge(token,amount);
        return R.ok().data("form",form);
    }
}

